# -*- coding: utf-8 -*-
from util.utils import Utils
from util.constants import Constants
from pprint import pprint


###################################################
# 用户模块
###################################################

class KangmaUser():

    # 客户登录、token
    def __init__(self, loginType, username, password):
        # CLIENT_TYPE_APP = "app"
        # CLIENT_TYPE_ADMIN_WEB_APP = "webApp"
        # CLIENT_TYPE_SUPPLIER_WEB_APP = "supplierApp"
        # CLIENT_TYPE_DATAVIEW = "dataView"
        # CLIENT_TYPE_CRM_WEB_APP = "crmWebApp"
        # CLIENT_TYPE_CRM_APP = "crmApp"
        if loginType == Constants.CLIENT_TYPE_APP:
            self.contentType = Constants.APP_CONTENTTYPE
            self.clientId = Constants.CLIENT_TYPE_APP
            self.clientSecret = Constants.CLIENT_TYPE_APP
            # 登录接口
            self.loginUrl = Constants.APP_LOGIN_URL
            # 获取登录者信息接口
            self.getLoginInfoUrl = Constants.APP_LOGIN_USER_INFO_URL
        elif loginType == Constants.CLIENT_TYPE_ADMIN_WEB_APP:
            self.contentType = Constants.WEB_CONTENTTYPE
            self.clientId = Constants.CLIENT_TYPE_ADMIN_WEB_APP
            self.clientSecret = Constants.CLIENT_TYPE_ADMIN_WEB_APP
            # 登录接口
            self.loginUrl = Constants.WEBAPP_LOGIN_URL
        elif loginType == Constants.CLIENT_TYPE_SUPPLIER_WEB_APP:
            self.contentType = Constants.WEB_CONTENTTYPE
            self.clientId = Constants.CLIENT_TYPE_SUPPLIER_WEB_APP
            self.clientSecret = Constants.CLIENT_TYPE_SUPPLIER_WEB_APP
            # 登录接口
            self.loginUrl = Constants.SUPPLIER_LOGIN_URL
            # 获取登录者信息接口
            self.getLoginInfoUrl = Constants.SUPPLIER_LOGIN_USER_INFO_URL
        elif loginType == Constants.CLIENT_TYPE_CRM_WEB_APP:
            self.contentType = Constants.WEB_CONTENTTYPE
            self.clientId = Constants.CLIENT_TYPE_CRM_WEB_APP
            self.clientSecret = Constants.CLIENT_TYPE_CRM_WEB_APP
            # 登录接口
            self.loginUrl = Constants.CRM_LOGIN_URL
        elif loginType == Constants.CLIENT_TYPE_CRM_APP:
            self.contentType = Constants.APP_CONTENTTYPE
            self.clientId = Constants.CLIENT_TYPE_CRM_APP
            self.clientSecret = Constants.CLIENT_TYPE_CRM_APP
            # 登录接口
            self.loginUrl = Constants.CRM_APP_LOGIN_URL

        login = self.login(username, password)
        self.authorization = Utils.getToken(loginType, login)
        if not self.authorization:
            pprint('获取token异常')
        # 登录信息
        self.loginUserInfo = self.getLoginInfo()

    # 运营商、供应商、crm登录
    # 获取登录者信息
    def login(self, username, password):
        data = {'username': username, 'password': password}
        r = Utils.post(self.contentType,
                       self.clientId,
                       self.clientSecret,
                       None,
                       self.loginUrl,
                       data)
        return Utils.getReturnData(r)

    # 获取登录信息
    def getLoginInfo(self):
        r = Utils.post(self.contentType,
                       self.clientId,
                       self.clientSecret,
                       self.authorization,
                       self.getLoginInfoUrl,
                       {})
        return Utils.getReturnData(r)


if __name__ == '__main__':
    # 测试app登录
    user = KangmaUser('app', '15900613490', '000000')
    # 登录信息
    pprint(user.authorization)
    pprint(user.loginUserInfo)
